{% extends "base.html" %}

{% block title %}导出管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            <i class="bi bi-images me-2"></i>导出图片管理
                        </h5>
                        <div>
                            <span class="badge bg-primary">{{ export_files|length }} 个文件</span>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    {% if error %}
                        <div class="alert alert-danger">
                            <i class="bi bi-exclamation-triangle me-2"></i>{{ error }}
                        </div>
                    {% endif %}
                    
                    {% if export_files %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>预览</th>
                                        <th>文件名</th>
                                        <th>格式</th>
                                        <th>大小</th>
                                        <th>创建时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for file in export_files %}
                                    <tr>
                                        <td>
                                            <div class="position-relative">
                                                <img src="{{ file.preview_url }}" 
                                                     alt="预览" 
                                                     class="img-thumbnail" 
                                                     style="width: 60px; height: 60px; object-fit: cover;"
                                                     onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
                                                <div class="d-flex align-items-center justify-content-center bg-light border rounded" 
                                                     style="width: 60px; height: 60px; display: none; font-size: 12px; color: #666;">
                                                    {{ file.format }}
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="fw-medium">{{ file.filename }}</div>
                                        </td>
                                        <td>
                                            <span class="badge bg-info">{{ file.format }}</span>
                                        </td>
                                        <td>
                                            <small class="text-muted">
                                                {% if file.size < 1024 %}
                                                    {{ file.size }} B
                                                {% elif file.size < 1024*1024 %}
                                                    {{ "%.1f"|format(file.size/1024) }} KB
                                                {% else %}
                                                    {{ "%.1f"|format(file.size/(1024*1024)) }} MB
                                                {% endif %}
                                            </small>
                                        </td>
                                        <td>
                                            <small class="text-muted">
                                                {{ file.created_at.strftime('%Y-%m-%d %H:%M') }}
                                            </small>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{{ file.download_url }}" 
                                                   class="btn btn-outline-primary" 
                                                   download>
                                                    <i class="bi bi-download"></i>
                                                </a>
                                                <button type="button" 
                                                        class="btn btn-outline-danger" 
                                                        onclick="deleteExport('{{ file.filename }}')">
                                                    <i class="bi bi-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-5">
                            <i class="bi bi-images display-1 text-muted"></i>
                            <h5 class="mt-3 text-muted">暂无导出文件</h5>
                            <p class="text-muted">完成图片定制后，导出的文件将显示在这里</p>
                            <a href="{{ url_for('intelligent_positioning') }}" class="btn btn-primary">
                                <i class="bi bi-robot me-1"></i>智能定制
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
async function deleteExport(filename) {
    if (!confirm('确定要删除这个文件吗？')) {
        return;
    }
    
    try {
        const response = await fetch(`/exports/${filename}/delete`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        });
        
        const result = await response.json();
        
        if (result.success) {
            location.reload();
        } else {
            alert('删除失败: ' + result.error);
        }
    } catch (error) {
        alert('删除失败: ' + error.message);
    }
}
</script>
{% endblock %}
